package com.yyfax.ph.personal.company;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewStub;
import android.widget.TextView;
import butterknife.BindView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.infrastructure.statistics.StatService;
import com.yyfax.ph.R;
import com.yyfax.ph.StatIDSet;
import com.yyfax.ph.base.BaseYYFaxActivity;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.base.dialog.DialogBase;
import com.yyfax.ph.common.utils.Constants;
import com.yyfax.ph.home.recommendloan.RecommendLoanActivity;
import com.yyfax.ph.personal.authority.bankcard.view.activity.BindCardActivity;
import com.yyfax.ph.personal.authority.bankcard.view.activity.GetBindCardActivity;
import com.yyfax.ph.personal.settings.BankCardPresenter;
import com.yyfax.ph.personal.settings.GetCompanyModel;
import com.yyfax.ph.personal.settings.GetCompanyPresenter;

/**
 * 我的渠道
 * Created by zhangxian on 2017/11/4.<br>
 * Copyright 2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public class ShowBindCompanyActivity extends BaseYYFaxActivity implements BindCompanyPresenter.View, BankCardPresenter.View, GetCompanyPresenter.View {
	private static final int LOADER_ID = 601;
	private static final int LOADER_ID_BANKCARD = 606;
	private static final int LOADER_ID_GET_COMPANY = 607;

	public final static String COMPANY = "company";
	public final static String STATUS = "status";
	public final static String PHONE = "phone";
	public static final String LAST_PAGE = "LAST_PAGE";//上一页来源
	public static final String FROM_HOME_FRAGMENT = "from_home_fragment";//首页推荐进来

	private String mLastPage; //进来之前的上一页：1.首页推荐, 2.助手 3.其他...

	@BindView(R.id.bind_company_process_vs)
	ViewStub mBindCompanyProcessVs;

	@BindView(R.id.bind_company_success_vs)
	ViewStub mBindCompanySuccessVs;

	TextView mPhoneTv;
	TextView mCompanyNameTv;


	private String mCompany;
	private String mPhone;
	private String mStatus;

	private BindCompanyPresenter mPresenter;
	private BankCardPresenter mBankCardPresenter;
	private GetCompanyPresenter mGetCompanyPresenter;

	public static void start(Activity aty, Intent intent) {
		aty.startActivity(intent);
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		switch (id) {
			case LOADER_ID:
				return new PresenterLoader<>(this, new BindCompanyPresenter.Generator());
			case LOADER_ID_BANKCARD:
				return new PresenterLoader<>(this, new BankCardPresenter.Generator());
			case LOADER_ID_GET_COMPANY:
				return new PresenterLoader<>(this, new GetCompanyPresenter.Generator());
			default:
				break;
		}
		return null;
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		switch (loader.getId()) {
			case LOADER_ID:
				mPresenter = (BindCompanyPresenter) data;
				mPresenter.attach(this);
				break;
			case LOADER_ID_BANKCARD:
				mBankCardPresenter = (BankCardPresenter) data;
				mBankCardPresenter.attach(this);
				break;
			case LOADER_ID_GET_COMPANY:
				mGetCompanyPresenter = (GetCompanyPresenter) data;
				mGetCompanyPresenter.attach(this);
				if (GetCompanyModel.STATUS_NONE.equals(mStatus)){
					mGetCompanyPresenter.getBindCompany();
				}
				break;
			default:
				break;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getSupportLoaderManager().initLoader(LOADER_ID, null, this);
		getSupportLoaderManager().initLoader(LOADER_ID_BANKCARD, null, this);
		getSupportLoaderManager().initLoader(LOADER_ID_GET_COMPANY, null, this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		getSupportLoaderManager().destroyLoader(LOADER_ID);
		getSupportLoaderManager().destroyLoader(LOADER_ID_BANKCARD);
		getSupportLoaderManager().destroyLoader(LOADER_ID_GET_COMPANY);
	}

	@Override
	public int getContentViewRes() {
		return R.layout.show_bind_company_activity;
	}


	@Override
	public void initContentView() {
		super.initContentView();
		setBtnRightTx(R.string.unbind_str);
		setBtnRightColor();
		setTitleVisible(true);
		setTitle(R.string.my_channel);
		int userType = YJPH.getInstance().getPreferenceManager().getUserType(0);
		View view;
		if (userType == Constants.TYPE_MOTHER_ACCOUNT) {
			//如果是企业账号
			view = mBindCompanySuccessVs.inflate();
			setBtnRightVisible(false);
			inflateView(view);
		} else {
			switch (mStatus) {
				//已绑定
				case GetCompanyModel.STATUS_BIND:
					view = mBindCompanySuccessVs.inflate();
					setBtnRightVisible(true);
					inflateView(view);
					break;
				//申请中
				case GetCompanyModel.STATUS_APPLYING:
					view = mBindCompanyProcessVs.inflate();
					inflateView(view);
					setBtnRightVisible(false);
					break;
			}
		}
	}

	void inflateView(View view) {
		if (null == view)
			return;
		mCompanyNameTv = (TextView) view.findViewById(R.id.company_name_tv);
		mPhoneTv = (TextView) view.findViewById(R.id.phone_tv);
		mCompanyNameTv.setText(mCompany);
		mPhoneTv.setText(mPhone);
	}

	@Override
	public void pretreatmentAfterSetBaseView() {
		super.pretreatmentAfterSetBaseView();
		Intent i = getIntent();
		if (i != null) {
			if (i.hasExtra(STATUS)) {
				mStatus = i.getStringExtra(STATUS);
			}
			if (i.hasExtra(COMPANY)) {
				mCompany = i.getStringExtra(COMPANY);
			}
			if (i.hasExtra(PHONE)) {
				mPhone = i.getStringExtra(PHONE);
			}
			if (i.hasExtra(LAST_PAGE)) {
				mLastPage = i.getStringExtra(LAST_PAGE);
			}
		}
	}

	@Override
	public void onBackBtnPressed() {
		super.onBackBtnPressed();
		finish();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}

	@Override
	public void onRightBtnPressed() {
		StatService.onEvent(YJPH.getInstance(), StatIDSet.EVENT_ID_E035);
		new DialogBase(this,
				getResources().getString(R.string.do_you_confirm_un_bind_company),
				"",
				getResources().getString(R.string.cancel),
				getResources().getString(R.string.unbind_str),
				new DialogBase.BottomTwoClickListener() {
					@Override
					public void onCancelClick(DialogBase dialog) {
						dialog.dismiss();
					}

					@Override
					public void onConfirmClick(DialogBase dialog) {
						dialog.dismiss();
						unBindCompany();
					}
				}
		).show();
	}

	public void unBindCompany() {
		mPresenter.bindCompany("");
	}

	@Override
	public void bindCompanySuccess(String tips) {
		dismissLoading();
		showTipsDialog(getString(R.string.bind_request_sent), new DialogBase.BottomOneClickListener() {
			@Override
			public void bottomOneClick(DialogBase dialog) {
				finish();
			}
		}, false);
	}

	@Override
	public void unbindCompanySuccess(String tips) {
		dismissLoading();
		showToast(R.string.unbind_success_toast);
		if (FROM_HOME_FRAGMENT.equals(mLastPage)) {//如果是从首页进来的，解绑成功获取实名认证信息
			mBankCardPresenter.getBindBankCardInfo();//判断有无实名认证
		} else {
			finish();
		}
	}

	@Override
	public void bindCompanyFailure(String tips) {
		dismissLoading();
		showToast(tips);
	}

	@Override
	public void getBankCardInfoSuccess(String name, String certId, String cardId, String bank, String bankCode) {
		if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(certId)) {//如果已经实名, 跳转到推荐首页
			RecommendLoanActivity.start(this);
		} else {//如果没有实名, 跳转到银行卡页面
			Intent intent = new Intent(this, BindCardActivity.class);
			intent.putExtra(GetBindCardActivity.NAME, name);
			intent.putExtra(GetBindCardActivity.CARD_ID, certId);
			intent.putExtra(BindCardActivity.LAST_PAGE, BindCardActivity.FROM_HOME_FRAGMENT);
			BindCardActivity.start(this, intent);
		}
		finish();
	}

	@Override
	public void getBankCardInfoFailed(String tips) {
		if (FROM_HOME_FRAGMENT.equals(mLastPage))
			finish();
	}

	@Override
	public void getCompanySuccess(String status, String company, String phone) {
		View view;
		switch (status) {
			//申请中
			case GetCompanyModel.STATUS_APPLYING:
				mCompany = company;
				mPhone = phone;
				view = mBindCompanyProcessVs.inflate();
				inflateView(view);
				setBtnRightVisible(false);
				break;
		}
	}
}
